//
//  smOrderSubmitController.m
//  shimaoHouse
//
//  Created by yst yst on 12-6-29.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smOrderSubmitController.h"
#import "smContratCreateController.h"
#import "smPaymentController.h"
#import "smMboSmTbOrder.h"
#import "smMboSmMboDB.h"
#import "smMboOrderSeq.h"
#import "smMboKeyGenerator.h"

@interface smOrderSubmitController ()<UITextFieldDelegate>

@property (nonatomic, strong) smMboSmTbOrder* order;
@property (nonatomic, strong) UILabel* palyLabel;
@property (nonatomic, strong) NSString* flag;
@end

@implementation smOrderSubmitController
@synthesize palyLabel = _palyLabel;
@synthesize flag = _flag;
@synthesize order = _order;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //更新时,显示订单编号
    if(self.flag==@"update")
    {
        UILabel* programnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(31, 80, self.view.bounds.size.width, 30)];
        [programnameLabel setBackgroundColor:[UIColor clearColor]];
        programnameLabel.text = [NSString stringWithFormat:@"订单编号：%@",[self.order id_]];
        programnameLabel.font = [UIFont systemFontOfSize:28];
        programnameLabel.textColor=[UIColor colorWithRed:126/255.0 green:126/255.0 blue:126/255.0 alpha:1.0];
        [self.view addSubview:programnameLabel];
    }

//    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(310, 140, 400, 30)];
//    [tempLabel setBackgroundColor:[UIColor clearColor]];
//    [tempLabel setText:@"世贸房产锦绣皇城"];
//    tempLabel.font = [UIFont systemFontOfSize:31];
//    tempLabel.textColor=[UIColor colorWithRed:78/255.0 green:103/255.0 blue:156/255.0 alpha:1.0];
//
//    [self.view addSubview:tempLabel];

    
    
    //后退
    UIButton* backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(5, 5, 118, 52);
    [backButton setImage:[UIImage imageNamed:@"back_button_up.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"back_button_down.png"] forState:UIControlStateHighlighted];
    backButton.backgroundColor=[UIColor clearColor];
    [backButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: backButton];
    
    UILabel* tempLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(365, 250, 400, 50)];
    [tempLabel1 setBackgroundColor:[UIColor clearColor]];
    tempLabel1.text = [NSString stringWithFormat:@"订单编号：%@",[self.order id_]];
    tempLabel1.font = [UIFont systemFontOfSize:22];
    tempLabel1.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel1];
    
    UILabel* tempLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(365, 300, 400, 50)];
    [tempLabel2 setBackgroundColor:[UIColor clearColor]];
    [tempLabel2 setText:@"创建时间：2012年6月30日"];
    tempLabel2.font = [UIFont systemFontOfSize:22];
    tempLabel2.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel2];
    
    UILabel* tempLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(365, 350, 400, 50)];
    [tempLabel3 setBackgroundColor:[UIColor clearColor]];
    [tempLabel3 setText:@"修改时间：2012年6月30日"];
    tempLabel3.font = [UIFont systemFontOfSize:22];
    tempLabel3.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel3];
    
    UILabel* tempLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(365, 400, 400, 50)];
    [tempLabel4 setBackgroundColor:[UIColor clearColor]];
    tempLabel4.text = [NSString stringWithFormat:@"服务人：%@",[self.order custName]];
    tempLabel4.font = [UIFont systemFontOfSize:22];
    tempLabel4.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel4];
    
    UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setFrame:CGRectMake(26, 760,282, 73)];
    [cancelButton setImage:[UIImage imageNamed:@"okOrderUP.png"] forState:UIControlStateNormal];
    [cancelButton setImage:[UIImage imageNamed:@"okOrderdown.png"] forState:UIControlStateHighlighted];
    [cancelButton addTarget:self action:@selector(commitClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelButton];
    
    
    UIButton* tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tempButton setFrame:CGRectMake(462, 760,282, 73)];
    [tempButton setImage:[UIImage imageNamed:@"toContUP.png"] forState:UIControlStateNormal];
    [tempButton setImage:[UIImage imageNamed:@"toContDOWN.png"] forState:UIControlStateHighlighted];
    [tempButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tempButton];
    
    
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"order_submit.png"]]];
        
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


//签订合同
-(void) buttonClick:(UIButton*) bt {
    smContratCreateController* contractCreateController = [[smContratCreateController alloc] init];
    [self.navigationController pushViewController:contractCreateController animated:YES];
}

//确认
-(void) commitClick:(UIButton*) bt {
    
    
    //修改
    if(self.flag == @"update"){
        @try {
            
          smMboSmTbOrder *orderTemp = [smMboSmTbOrder findByPrimaryKey: [self.order id_]];
            [orderTemp setCustName:[self.order custName]];
            [orderTemp setCustAddress:[self.order custAddress]];
            [orderTemp setHouseId:[self.order houseId]];
            [orderTemp setEarnestMoney:[self.order earnestMoney]];
            [orderTemp setTotalMoney:[self.order totalMoney]];
            [orderTemp save]; 
            [orderTemp submitPending];
            [smMboSmMboDB synchronize];
            
            UIAlertView *updateAlert = [[UIAlertView alloc] initWithTitle:@"订单修改" message:@"修改成功"
                                                                 delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [updateAlert show];

                 
        }
        @catch (NSException *exception) {
            NSLog(@"%@: %@", [exception name],[exception reason]);
            return;
        }
        
    }
    //创建
    else{

      @try { 
          //seq 菊花
//          smMboOrderSeq * seq;
//          [smMboSmMboDB beginSynchronize];
//          SUPObjectList* list=[smMboOrderSeq findAll];
//          if ([list length] > 0)
//          {
//              for (seq in list)
//              {
//                  NSString* temp1 = [NSString stringWithFormat:@"seq is %d", [seq orderSeqVal]];
//                  NSLog(temp1,nil);
//              }
//          }
        [self.order create:([NSNumber numberWithInt:[@"410" intValue]])];    
        [self.order submitPending];
        [smMboSmMboDB synchronize];
        
      }
      @catch (NSException *exception) {
        NSLog(@"%@: %@", [exception name],[exception reason]);
        return;
      }

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"订单创建" message:@"下单成功"
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        
      [alert show];	
    }
    
}

//返回
-(void) backClick:(UIButton*) bt {     
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) setitems: (smMboSmTbOrder *) obj fl:(NSString *) flag {
    
    self.order=obj;
    self.flag = flag;
    
}


@end
